package cz.worktracker.validators;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

/**
 * PasswordsValidator
 * Validátor kontroluje shodnost zadaných hesel
 * @since 2010-04-22
 * @author kyjovsky
 */
public class PasswordsValidator implements Validator {

    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException
    {
        String confirm = (String) value;

        String passwordId = (String) component.getAttributes().get("passwordID");
        UIInput passwordInput = (UIInput) context.getViewRoot().findComponent(passwordId);
        String password = (String) passwordInput.getValue();
        if (password == null) return;
        if(password.length() < 3) return;

        if (!password.equals(confirm)) {
            FacesMessage message = new FacesMessage(((UIInput)component).getValidatorMessage());
            throw new ValidatorException(message);
        }
    }

}